<?php

class GetWxData
{
    private $appid = 'wx16320d7479a5774c';
    private $appsecret = 'e111c965e8d4ba6a2ff1a99724f6d573';

    /**
     * 1、获取微信用户信息，判断有没有code，有使用code换取access_token，没有去获取code。
     * @return array 微信用户信息数组
     */
    public function get_user_all()
    {
        if (!isset($_GET['code'])) {//没有code，去微信接口获取code码
            $callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];//微信服务器回调url，这里是本页url
            $this->get_code($callback);
        } else {//获取code后跳转回来到这里了
            $code = $_GET['code'];
            $data = $this->get_access_token($code);//获取网页授权access_token和用户openid
            $data_all = $this->get_user_info($data['access_token'], $data['openid']);//获取微信用户信息
            return $data_all;
        }
    }

    //接口调用凭证接口token获取
    public function access_token()
    {
        $appid = $this->appid;
        $appsecret = $this->appsecret;
//        $mysql = new MySqli('121.37.171.2', 'sql_5p8_fyh5p8_c', 'XKjeShLWexejJ32A', 'sql_5p8_fyh5p8_c');
//        $sql = "select * from dyspw_access where id = 1";
//        $access_token = mysqli_query($mysql, $sql);
//        $access_token_info = $access_token->fetch_all(MYSQLI_ASSOC);
//        if (($access_token_info[0]['time'] > time()) && !empty($access_token_info[0]['time'])) {
//            $token = $access_token_info[0]['param'];
//            return $token;
//        } else {
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
        $user = json_decode(file_get_contents($url), true);
        $mysql = new MySqli('121.37.171.2', 'sql_5p8_fyh5p8_c', 'XKjeShLWexejJ32A', 'sql_5p8_fyh5p8_c');
//            $row_sql1 = $mysql->query("update dyspw_access set param = '" . $user['access_token'] . "' where id = 1");
        $time = time() + 7200;
//            $row_sql2 = $mysql->query("update dyspw_access set time = '" . $time . "' where id = 1");
        return $user['access_token'];
//        }
    }

    /**
     * 2、用户授权并获取code
     * @param string $callback 微信服务器回调链接url
     */
    private function get_code($callback)
    {
        $appid = $this->appid;
        $scope = 'snsapi_userinfo';
        $state = md5(uniqid(rand(), TRUE));//唯一ID标识符绝对不会重复
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . urlencode($callback) . '&response_type=code&scope=' . $scope . '&state=' . $state . '&connect_redirect=1#wechat_redirect';
        // echo $url;exit;
        header("Location:$url");
    }

    /**
     * 3、使用code换取access_token
     * @param string 用于换取access_token的code，微信提供
     * @return array access_token和用户openid数组
     */
    private function get_access_token($code)
    {
        $appid = $this->appid;
        $appsecret = $this->appsecret;
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
        $user = json_decode(file_get_contents($url));
        if (isset($user->errcode)) {
            echo 'error:' . $user->errcode . '<hr>msg  :' . $user->errmsg;
            exit;
        }
        $data = json_decode(json_encode($user), true);//返回的json数组转换成array数组
        $data['expire_time'] = time() + $data['expires_in'];
        return $data;
    }

    /**
     * 4、使用access_token获取用户信息
     * @param string access_token
     * @param string 用户的openid
     * @return array 用户信息数组
     */
    private function get_user_info($access_token, $openid)
    {
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
        $user = json_decode(file_get_contents($url));
        if (isset($user->errcode)) {
            echo 'error:' . $user->errcode . '<hr>msg  :' . $user->errmsg;
            exit;
        }
        $data = json_decode(json_encode($user), true);//返回的json数组转换成array数组
        return $data;
    }

    //对外开放，获取access_token
    public function get_access_token_open()
    {
        if (!isset($_GET['code'])) {//没有code，去微信接口获取code码
            $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            //$callback = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];//微信服务器回调url，这里是本页url
            $callback = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];//微信服务器回调url，这里是本页url
            $this->get_code($callback);
        } else {//获取code后跳转回来到这里了
            $code = $_GET['code'];
            $data = $this->get_access_token($code);//获取网页授权access_token和用户openid
            return $data;
        }
    }
}

$script = new GetWxData();
$access_token = $script->access_token();
$mysql = new MySqli('121.37.171.2', 'sql_5p8_fyh5p8_c', 'XKjeShLWexejJ32A', 'sql_5p8_fyh5p8_c');
$sql = "select * from dyspw_member";
$list = mysqli_query($mysql, $sql);
$user_list = $list->fetch_all(MYSQLI_ASSOC);
foreach ($user_list as $key => $value) {
//    var_dump($user_list[$key]['openid'] . '777' . $access_token);
//    exit;
    $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $access_token . '&openid=' . $user_list[$key]['openid'] . '&lang=zh_CN';
    $user = json_decode(file_get_contents($url), true);
    $unionid = $user['unionid'];
    var_dump($user);exit;
}


